package han.yang.service.friendship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import han.yang.common.ResponseVO;
import han.yang.common.enums.DelFlagEnum;
import han.yang.common.enums.FriendShipErrorCode;
import han.yang.common.exception.ApplicationException;
import han.yang.service.friendship.dao.ImFriendShipGroupEntity;
import han.yang.service.friendship.dao.mapper.ImFriendShipGroupMapper;
import han.yang.service.friendship.dao.mapper.ImFriendShipGroupMemberMapper;
import han.yang.service.friendship.model.req.AddFriendShipGroupMemberReq;
import han.yang.service.friendship.model.req.AddFriendShipGroupReq;
import han.yang.service.friendship.model.req.DeleteFriendShipGroupReq;
import han.yang.service.friendship.service.ImFriendShipGroupMemberService;
import han.yang.service.friendship.service.ImFriendShipGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class ImFriendShipGroupServiceImpl implements ImFriendShipGroupService {

    @Resource
    ImFriendShipGroupMapper imFriendShipGroupMapper;

    @Resource
    ImFriendShipGroupMemberService imFriendShipGroupMemberService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseVO<?> addGroup(AddFriendShipGroupReq req) {
        QueryWrapper<ImFriendShipGroupEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",req.getAppId())
                .eq("group_name",req.getGroupName())
                .eq("from_id",req.getFromId());
        ImFriendShipGroupEntity entity = imFriendShipGroupMapper.selectOne(queryWrapper);
        if (entity != null && entity.getDelFlag() == DelFlagEnum.NORMAL.getCode()){
            // 分组以及存在，无需创建
            throw new ApplicationException(FriendShipErrorCode.FRIEND_SHIP_GROUP_IS_EXIST);
        }
        if (entity != null && entity.getDelFlag() == DelFlagEnum.DELETE.getCode()){
            entity.setDelFlag(DelFlagEnum.NORMAL.getCode());
            entity.setUpdateTime(System.currentTimeMillis());
            imFriendShipGroupMapper.updateById(entity);
        }
        // 插入分组
        entity = new ImFriendShipGroupEntity();
        entity.setAppId(req.getAppId());
        entity.setFromId(req.getFromId());
        entity.setGroupName(req.getGroupName());
        entity.setCreateTime(System.currentTimeMillis());
        try{
            int insert = imFriendShipGroupMapper.insert(entity);
            if (insert != 1){
                return ResponseVO.errorResponse(FriendShipErrorCode.FRIEND_SHIP_GROUP_CREATE_ERROR);
            }
            if (req.getToIds() != null && req.getToIds().size() > 0){
                // 如果提前携带了toId，插入tiId的记录
                AddFriendShipGroupMemberReq addFriendShipGroupMemberReq = new AddFriendShipGroupMemberReq();
                addFriendShipGroupMemberReq.setFromId(req.getFromId());
                addFriendShipGroupMemberReq.setGroupName(req.getGroupName());
                addFriendShipGroupMemberReq.setToIds(req.getToIds());
                addFriendShipGroupMemberReq.setAppId(req.getAppId());
                imFriendShipGroupMemberService.addGroupMember(addFriendShipGroupMemberReq);
                return ResponseVO.successResponse();
            }
        }catch (Exception e){
            return ResponseVO.errorResponse(FriendShipErrorCode.FRIEND_SHIP_GROUP_CREATE_ERROR);
        }
        return null;
    }

    @Override
    public ResponseVO getGroup(String fromId, String groupName, Integer appId) {
        QueryWrapper<ImFriendShipGroupEntity> query = new QueryWrapper<>();
        query.eq("group_name", groupName);
        query.eq("app_id", appId);
        query.eq("from_id", fromId);
        query.eq("del_flag", DelFlagEnum.NORMAL.getCode());

        ImFriendShipGroupEntity entity = imFriendShipGroupMapper.selectOne(query);
        if (entity == null) {
            return ResponseVO.errorResponse(FriendShipErrorCode.FRIEND_SHIP_GROUP_IS_NOT_EXIST);
        }
        return ResponseVO.successResponse(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseVO deleteGroup(DeleteFriendShipGroupReq req) {
        req.getGroupName().forEach(info -> {
            QueryWrapper<ImFriendShipGroupEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("app_id",req.getAppId());
            queryWrapper.eq("from_id",req.getFromId());
            queryWrapper.eq("group_name",info);
            queryWrapper.eq("del_flag",DelFlagEnum.NORMAL.getCode());
            ImFriendShipGroupEntity groupEntity = imFriendShipGroupMapper.selectOne(queryWrapper);
            if (groupEntity != null){
                groupEntity.setUpdateTime(System.currentTimeMillis());
                groupEntity.setDelFlag(DelFlagEnum.DELETE.getCode());
                imFriendShipGroupMapper.updateById(groupEntity);
                // 清除分组成员
                imFriendShipGroupMemberService.clearGroupMember(groupEntity.getGroupId());
            }
        });
        return ResponseVO.successResponse();
    }
}
